package com.colorlight.cloud.grandshow.entity.instructions;


import com.colorlight.cloud.grandshow.api.GrandShowEntityScheduleActions;
import com.colorlight.cloud.grandshow.entity.po.AuthorizationDetail;
import com.colorlight.cloud.grandshow.entity.po.DeviceAuthResultInfo;
import com.colorlight.cloud.grandshow.entity.po.SenderAuthResultInfo;
import com.colorlight.cloud.grandshow.entity.po.Time;
import com.colorlight.iot.proto.grandshow.instruction.GrandShowInstruction;
import com.google.protobuf.ByteString;
import com.google.protobuf.InvalidProtocolBufferException;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.io.Serializable;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author Demon
 * @description 定时加密
 * @date 22/3/2024  上午8:59
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Slf4j
public class TimingEncrypt implements GrandShowEntityScheduleActions {
    private InputMsg input;
    private OutputMsg output;

    @Override
    public ByteString objectConvertBytes() {
        final GrandShowInstruction.TimingEncrypt.InputMsg.Builder inputMsg =
                GrandShowInstruction.TimingEncrypt.InputMsg.newBuilder();
        inputMsg.setTime(this.getInput().getTime().toProto());
        inputMsg.setDetail(this.getInput().getAuthorizationDetail().toProto());
        final GrandShowInstruction.TimingEncrypt.Builder builder =
                GrandShowInstruction.TimingEncrypt.newBuilder();
        builder.setInput(inputMsg);
        return builder.build().toByteString();
    }

    @Override
    public TimingEncrypt byteConvertObject(ByteString bytes) {
        GrandShowInstruction.TimingEncrypt timingEncrypt = null;
        try {
            timingEncrypt = GrandShowInstruction.TimingEncrypt.parseFrom(bytes);
        } catch (InvalidProtocolBufferException e) {
            log.error("", e);
        }
        Objects.requireNonNull(timingEncrypt);

        //上报消息为空
        GrandShowInstruction.TimingEncrypt.OutputMsg output = timingEncrypt.getOutput();
        TimingEncrypt.OutputMsg outputMsg = new TimingEncrypt.OutputMsg();
        outputMsg.setDeviceAuthResultInfo(new DeviceAuthResultInfo().formProto(output.getDeviceAuthResultInfo()));
        List<SenderAuthResultInfo> collect = output.getSenderAuthResultInfoList().stream().map(e -> new SenderAuthResultInfo().formProto(e)).collect(Collectors.toList());
        outputMsg.setSenderAuthResultInfo(collect);
        this.setOutput(outputMsg);
        return this;
    }

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    @Builder
    public static class InputMsg implements Serializable {
        private Time time;
        private AuthorizationDetail authorizationDetail;
    }

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    @Builder
    public static class OutputMsg implements Serializable {
        private DeviceAuthResultInfo deviceAuthResultInfo;
        private List<SenderAuthResultInfo> senderAuthResultInfo;
    }


}
